package com.wjr.braveheart.ui.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.wjr.braveheart.eventbus.EventBusUtils;
import com.wjr.braveheart.eventbus.MessageEvent;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * UI相关Fragment
 * 自定义相关配置
 */
public abstract class AppFragment extends BaseLazyFragment {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (isRegisterEventBus()) {
            EventBusUtils.register(this);
        }
    }

    @Override
    protected void unbindView(View view) {
        if (isRegisterEventBus()) {
            EventBusUtils.unregister(this);
        }
    }

    /**
     * 需要接收事件 重写该方法 并返回true
     */
    protected boolean isRegisterEventBus() {
        return false;
    }

    /**
     * Called in Android UI's main thread
     * 子类接受事件 重写该方法
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBus(MessageEvent event) {
    }

    /**
     * Called in a separate thread
     * 异步执行，另起线程操作。
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.ASYNC)
    public void onEventAsync(MessageEvent event) {
    }

    /**
     * Called in the background thread
     * 后台进程，处理如保存到数据库等操作。
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.BACKGROUND)
    public void onEventBackground(MessageEvent event) {
    }

    /**
     * Called in the same thread (default)
     * 在同一个线程中执行
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.POSTING)
    public void onEventPosting(MessageEvent event) {
    }
}
